module.exports = function(Mogo) {
  const request = Mogo.request;
  Mogo.Model = class MogoModel {
    constructor(className) {
      if (!className) throw "没有定义类名";
      // 判断是否初始化
      if (!Mogo._config.domain) throw "Mogo没有初始化";
      this.className = className;
      this.query = {};
    }
    // 模型关联
    with(attr) {
      this.query.with = attr;
      return this;
    }
    // 模型查询
    where(attr) {
      this.query.where = attr;
      return this;
    }
    // 排序
    order(attr) {
      this.query.order = attr;
      return this;
    }
    // 关联统计
    withCount(attr) {
      this.query.withCount = attr;
      return this;
    }
    // 属性
    attr(attr) {
      this.query.attr = attr;
      return this;
    }
    // 查询列表
    async find(page = 1) {
      this.query.page = page;
      const option = {
        url: `/mogo-sdk/class/${this.className}`,
        method: "post",
        data: this.query
      };
      this.query = {};
      return await request(option);
    }
    // 查询一个
    async findOne(id) {
      const option = {
        url: `/mogo-sdk/class/${this.className}/${id}`,
        method: "post",
        data: this.query
      };
      this.query = {};
      return await request(option);
    }

    // 执行模型中的函数
    async execute(method, data = {}) {
      const option = {
        url: `/mogo-sdk/${this.className}/method/${method}`,
        method: "post",
        data: data
      };
      return await request(option);
    }
  };
};
